﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanLyDuoc.HeThongNhapXuat
{
    public class PhieuYeuCauXuatVatTu:DBInteractionBase
    {
        // Phiếu yêu cầu xuất vật tư
        public string MaPhieu { get; set; }
        public DateTime NgayYeuCau { get; set; }
        public string NguoiYeuCau { get; set; }
        public string MaKhoYeuCau { get; set; }
        public string MaKhoXuat { get; set; }
        public string DienGiai { get; set; }
        public string MaNguoiXetDuyet { get; set; }
        public int IsXetDuyet { get; set; }
        public string NguoiTao { get; set; }
        // Phiếu yêu cầu xuất vật tư chi tiết
        public string ID { get; set; }
        public string MaVatTu { get; set; }
        public string MaDonVi { get; set; }
        public int SoLuong { get; set; }

        public bool PhieuYeuCauXuatVatTu_Insert(PhieuYeuCauXuatVatTu obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu_Output", SqlDbType.VarChar)
                    {
                        Direction = ParameterDirection.Output,
                        Size = 50
                    });
                    dbcmd.Parameters.Add(new SqlParameter("@NgayYeuCau", obj.NgayYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiYeuCau", obj.NguoiYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoYeuCau", obj.MaKhoYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoXuat", obj.MaKhoXuat));
                    dbcmd.Parameters.Add(new SqlParameter("@DienGiai", obj.DienGiai));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                    dbcmd.ExecuteNonQuery();
                    obj.MaPhieu = dbcmd.Parameters["@MaPhieu_Output"].Value.ToString();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuYeuCauXuatVatTu_Update(PhieuYeuCauXuatVatTu obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", obj.MaPhieu));
                    dbcmd.Parameters.Add(new SqlParameter("@NgayYeuCau", obj.NgayYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiYeuCau", obj.NguoiYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoYeuCau", obj.MaKhoYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoXuat", obj.MaKhoXuat));
                    dbcmd.Parameters.Add(new SqlParameter("@DienGiai", obj.DienGiai));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuYeuCauXuatVatTu_Delete(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuYeuCauXuatVatTu_UpdateDuyetPhieu(string maphieu, string manguoixetduyet, int isxetduyet, string ghichu)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_UpdateDuyetPhieu", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", maphieu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaNguoiXetDuyet", manguoixetduyet));
                    dbcmd.Parameters.Add(new SqlParameter("@IsXetDuyet", isxetduyet));
                    dbcmd.Parameters.Add(new SqlParameter("@GhiChu", ghichu));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable PhieuYeuCauXuatVatTu_SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_PhieuYeuCauXuatVatTu_SelectById: " + ex.Message);
            }
        }

        public DataTable PhieuYeuCauXuatVatTu_SelectAll(DateTime tungay, DateTime denngay, string makho, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TuNgay", tungay));
                    dbcmd.Parameters.Add(new SqlParameter("@DenNgay", denngay));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKho", makho));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_PhieuYeuCauXuatVatTu_SelectAll: " + ex.Message);
            }
        }

        public DataTable PhieuYeuCauXuatVatTu_SelectKhoXuat(string makhoa)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_SelectKhoXuat", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", makhoa));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_PhieuYeuCauXuatVatTu_SelectKhoXuat: " + ex.Message);
            }
        }

        public bool PhieuYeuCauXuatVatTu_ChiTiet_Insert(PhieuYeuCauXuatVatTu obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_ChiTiet_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", obj.MaPhieu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaVatTu", obj.MaVatTu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaDonVi", obj.MaDonVi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoLuong", obj.SoLuong));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuYeuCauXuatVatTu_ChiTiet_Update(PhieuYeuCauXuatVatTu obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_ChiTiet_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", obj.ID));
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", obj.MaPhieu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaVatTu", obj.MaVatTu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaDonVi", obj.MaDonVi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoLuong", obj.SoLuong));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuYeuCauXuatVatTu_ChiTiet_Delete(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_ChiTiet_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable PhieuYeuCauXuatVatTu_ChiTiet_SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_ChiTiet_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_PhieuYeuCauXuatVatTu_ChiTiet_SelectById: " + ex.Message);
            }
        }

        public DataTable PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu(string maphieu)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieuYeuCauXuatVatTu", maphieu));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu: " + ex.Message);
            }
        }
    }
}
